// pages/user_center/contactus/contactus.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    contactData: [],
    sendtext: '',
    toView:'',

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getcontactData()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    // ('scrollpage').scrollTop(('scrollpage')[0].scrollHeight);
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  // onPullDownRefresh: function () {

  // },

  /**
   * 页面上拉触底事件的处理函数
   */
  // onReachBottom: function () {

  // },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {

  // }
  callPhone: function () {
    var phone = '0512-62881403'
    wx.makePhoneCall({
      phoneNumber: phone //仅为示例，并非真实的电话号码
    });
  },
  getcontactData: function () {
    var that = this
    // wx.showToast({
    //   title: '正在加载...',
    //   icon: 'loading',
    //   duration: 10000
    // });

    var userInfo = wx.getStorageSync('userInfo')
    console.log(userInfo)
    var password = wx.getStorageSync('password')
    var tel = ''
    if (userInfo.USerType == '1' || userInfo.USerType == '2') {
      tel = userInfo.Telphone
    }
    if (userInfo.USerType == '3' || userInfo.USerType == '4') {
      tel = userInfo.sTel
    }
    if (userInfo.USerType == '5'){
      tel = userInfo.cTel
    }
    wx.request({
      url: 'https://www.lanshuicar.com/Server/Advice2Ls.ashx',
      data: {
        utel: tel,
        upwd: password,
        type: userInfo.USerType,
        act: 'getl',
      },
      method: 'GET',
      success: function (res) {
        // wx.hideToast()
        var data = res.data
        console.log(data)
        if (data.code == 0) {
          console.log(data.data)
          that.setData({
            contactData: data.data
          })
        var last = data.data.length
        var id = 'a' + that.data.contactData[last-1].ID
        that.setData({
          toView:id,
        })

          // wx.createSelectorQuery().select('scrollpage').boundingClientRect(function (rect) {
            // wx.pageScrollTo({
            //   scrollTop: 1000,
            // })
          // })

        }


      },
      fail: function (res) {

      }
    })
  },

  sendtextchange: function (e) {
    var sendtext = e.detail.value
    console.log(sendtext)
    this.setData({
      sendtext: sendtext
    })

  },

  send: function () {
    var that = this
    if (that.data.sendtext == '') {
      wx.showToast({
        title: '请输入留言内容',
        icon: 'none',
      })
      return;
    }
    var userInfo = wx.getStorageSync('userInfo')
    var password = wx.getStorageSync('password')
    var tel = ''
    if (userInfo.USerType == '1' || userInfo.USerType == '2') {
      tel = userInfo.Telphone
    }
    if (userInfo.USerType == '3' || userInfo.USerType == '4' || userInfo.USerType == '5') {
      tel = userInfo.sTel
    }
    wx.request({
      url: 'https://www.lanshuicar.com/Server/Advice2Ls.ashx',
      data: {
        utel: tel,
        upwd: password,
        type: userInfo.USerType,
        act: 'add',
        msg: that.data.sendtext

      },
      method: 'GET',
      success: function (res) {
        console.log(res)
        if (res.data.code == 0) {
          wx.showToast({
            title: '发送成功',
            icon:'none'
          })
          that.setData({
            sendtext: ''
          })
          that.getcontactData()
        }
      }, fail: function (res) {

      }
    })


  },



})